package com.zhuoyue.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

import com.zhuoyue.biz.core.api.dto.rep.EzvizTokenRepDTO;
import com.zhuoyue.biz.core.api.service.impl.EzvizTokenService;
import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.aop.auth.User;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.zhuoyue.biz.core.api.domain.EzvizTokenDO;
import com.zhuoyue.common.dto.ControllerResult;

/**
 * @author wjming
 * @ClassName: AppEzvizTokenController
 * @Description: 1、摄像头主账号信息获取  2、家庭和子账号关系信息的CURD
 * @Date 2021/2/14 19:42
 */
@RestController
@Scope("prototype")
@Validated
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))

public class AppEzTokenController {
    private final EzvizTokenService ezvizTokenService;


    /**
     * @Description: 获取该家庭在萤石云平台上注册的信息
     * @param: homeId
     * @param: request
     * @return: com.zhuoyue.common.dto.ControllerResult
     * @author:wjming
     * @Date:2021/4/17 13:28
     */
    @GetMapping("/api/ezviz/token/get")
    @CheckLogin
    public ControllerResult getHomeAndEzChildAccountRelationMessage(@NotBlank(message = "家庭ID不能为空") String homeId,
                                                                    HttpServletRequest request) {

        User user = (User) request.getAttribute("user");
        log.info("request -> user:[{}_{}] [getHomeAndEzChildAccountRelationMessage] -> [homeId:{}]", user.getAccount(), user.getOrgCode(), homeId);
        EzvizTokenDO et = new EzvizTokenDO();
        et.setHomeId(homeId);
        EzvizTokenRepDTO ezvizTokenRepDTO = ezvizTokenService.getEzvizTokenByHomeId(et);
        log.info("result -> [getHomeAndEzChildAccountRelationMessage] -> [{}]", ezvizTokenRepDTO);
        return ControllerResult.toReturnDataSuccess("获取成功", ezvizTokenRepDTO);
    }

    /**
     * @Description: 修改或绑定该家庭中莹石子账号ID
     * @param: request
     * @param: homeId
     * @param: ezId
     * @return: EzvizTokenRepDTO
     * @author:wjming
     * @Date:2021/4/17 13:28
     */
    @GetMapping("/api/ezviz/token/deal")
    @CheckLogin
    public ControllerResult addHomeAndEzChildAccountRelationMessage(HttpServletRequest request, @NotBlank(message = "家庭ID不能为空") String homeId,
                                                                    @RequestParam(value = "ezvizId")
                                                                    @NotBlank(message = "子账号ID不能为空") String ezId) {

        User user = (User) request.getAttribute("user");
        log.info("request -> user:[{}_{}] [addHomeAndEzChildAccountRelationMessage] -> [homeId:{},ezId:{}]", user.getAccount(), user.getOrgCode(), homeId, ezId);
        EzvizTokenDO et = EzvizTokenDO.builder().homeId(homeId).appUserId(user.getId()).ezvizId(ezId).build();
        //根据APP用户id查询用户是否已经绑定
        EzvizTokenRepDTO ezvizTokenRepDTO = ezvizTokenService.saveOrUpdateChildIdAndRepChildMessage(et);
        log.info("result -> [addHomeAndEzChildAccountRelationMessage][EzvizTokenRepDTO:{}]", ezvizTokenRepDTO);
        return ControllerResult.toReturnDataSuccess("成功", ezvizTokenRepDTO);
    }


    /**
     * 向莹石云获取主账号token信息,并且保存在缓存中
     *
     * @param request
     * @return
     */
    @GetMapping("/api/ezvizUser/token/get")
    @CheckLogin
    public ControllerResult getMainAccountNumberToken(HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        String str = ezvizTokenService.getEzTokenByUser(user.getOrgCode());
        log.info("result -> [getMainAccountNumberToken] -> [主账号token信息:{}]", str);
        return ControllerResult.toReturnDataSuccess("获取成功", str);
    }

    /**
     * 萤石token信息第三方获取
     *
     * @param orgCode
     * @return
     */
    @GetMapping("/third/api/ezvizUser/token/get")
    public ControllerResult thridGetMainAccountNumberToken(String orgCode) {
        String str = ezvizTokenService.getEzTokenByUser(orgCode);
        log.info("result -> [getMainAccountNumberToken] -> [主账号token信息:{}]", str);
        return ControllerResult.toReturnDataSuccess("获取成功", str);
    }
}
	
	

